$(function(){
	var userid = ym.getQueryStringByName('userid');
	var exchangeId = ym.getQueryStringByName('exchangeId');
	var geta = ym.getQueryStringByName('a');
	if(geta == 0){
		$('#goBackIcon').attr('href','javascript:history.go(-1)');
	}else{
		$('#goBackIcon').attr('href','taolunhuodong.html');
	}
	
	ym.ajax({
        url: "/communication/quertCommunicationDetail",
        param:{
            exchangeId:exchangeId,
            userId:userid
        },
        success: function (data) {
           var data=JSON.parse(data);
           if(data.res == 1){
           		console.log(data);
          		var html = "";
          		$('.teblack').text(data.obj[0].exchange.userName+"的话题");
          		for(var i=0;i<data.obj.length;i++){
          			html += '<ul class="discusstopic content-tab" id="top-tab">';
          			html += '<li>';
          			html += '<div class="topic">';
          				html += '<img src="'+data.obj[i].exchange.headimgurl+'">';
          				html += '<div class="introduce">';
          					html += '<h4 class="color0C54F8 font15">'+data.obj[i].exchange.userName+'</h4>';
          					 html += '<span>'+data.obj[i].exchange.createTimeString+'</span>';
          					// html += '<span>123</span>';
          				html += '</div>';
          				html += '<div class="reply textright">';
          					html += '<img style="margin-top:0.3rem" src="../img/pinglun.png">';
          					html += '<span>'+data.obj[i].answersCount+'</span>';
          				html += '</div>';
          			html += '</div>';
          			html += '<p style="font-size:1.5rem">'+data.obj[i].exchange.ecchangeMessage+'</p>';

          			html += '<ul class="am-avg-sm-3 am-text-center kcyy1 font1 picView-magnify-list" style="margin-left:3.8rem">';
          			if(data.obj[i].ymAttachments.length > 0){
          				for(var j=0;j<data.obj[i].ymAttachments.length;j++){
          					html += '<li>'+
                            "<a href='javascript:void(0)' data-magnify='gallery' data-group='g1' data-src='"+data.obj[i].ymAttachments[j].attachmentUrl+"' data-caption=''>"+
                            '<img src="'+data.obj[i].ymAttachments[j].attachmentUrl+'">'+
                            "</a>"+
                            '</li>';
          				}
          			}
          			html += '</ul>';
               		html += '</ul>';
          			html += '<ul class="backwhite replycomment" style="margin-top: 1rem;">';
          			for(var k=0;k<data.obj[i].ymAnswers.length;k++){
          				var time = data.obj[i].ymAnswers[k].createTimeString;
          				time = time.substr(5,0);
          				if(data.obj[i].ymAnswers[k].parentId == 0){
          					html += '<li class="padding10">';
          					    html += '<div class="number xxpl font15 colorb6bcce">';
          							html += '<img src="'+data.obj[i].ymAnswers[k].headimgurl+'">';
          							html += '<div class="w90 am-margin-left-sm">';
          								html += '<span class="color0C54F8">'+data.obj[i].ymAnswers[k].userName+'</span>';
          								html += '<p class="font13 margintop02">'+data.obj[i].ymAnswers[k].createTimeString+'</p>';
          								html += '<p class="color333 am-padding-left-xs margint10 lunls">'+data.obj[i].ymAnswers[k].answerDes+'</p>';
          								html += '<div class="am-margin-top-xs zitibottom">';
	          								html += '<span class="font13">'+data.obj[i].ymAnswers[k].address+'</span>'
	          								html += '<span class="plgr textright"><img src="../img/pinglun.png"></span>';
          								html += '<div>';
          							html += '</div>';
          							html += '<span class="plgr textright" id="'+data.obj[i].ymAnswers[k].answerDes+'"></span>';
          						html += '</div>';  
					        	html += '<div style="display:none">';
					        		html += '<p class="usna">'+data.obj[i].ymAnswers[k].userName+'</p>';
					       	 		html += '<p class="usid">'+data.obj[i].ymAnswers[k].answerUserId+'</p>';
					        	html += '</div>';
					        html += '</li>';
					    }else{
          					html += '<li class="padding10">';
          						html += '<div class="number xxpl font15 colorb6bcce">';
          							html += '<img src="'+data.obj[i].ymAnswers[k].headimgurl+'">';
          							html += '<div class="w90 am-margin-left-sm ">';
          								html += '<span class="color0C54F8">'+data.obj[i].ymAnswers[k].userName+'</span>';
          								html += '<p class="font13 margintop02">'+data.obj[i].ymAnswers[k].createTimeString+'</p>';
          								html += '<p class="color333 margint10 am-padding-left-sm lunls"><span>回复<i class="color69e600 am-margin-left-xs">'+data.obj[i].ymAnswers[k].parentName+'</i>:</span>'+data.obj[i].ymAnswers[k].answerDes+'</p>';
          								html += '<div class="am-margin-top-xs zitibottom">'
	          								html += '<span class="font13">'+data.obj[i].ymAnswers[k].address+'</span>'
	          								html += '<span class="plgr textright"><img src="../img/pinglun.png"></span>';
          								html += '</div>'
          							html += '</div>';
          						html += '</div>';   	
					        	html += '<div style="display:none">';
					        		html += '<p class="usna">'+data.obj[i].ymAnswers[k].userName+'</p>';
					       	 		html += '<p class="usid">'+data.obj[i].ymAnswers[k].answerUserId+'</p>';
					        	html += '</div>';
					        html += '</li>';
					    }
          			}
          			html += '</ul>';
          			
          		}
          		$('.cons').append(html);
          	}
        }
    });
    
    var urls = "http://vc.nuodk.cn"+window.location.pathname+"?userid="+userid+"&exchangeId="+exchangeId;
    console.log(urls);
    var a =0;
    $('body').on('click','.plgr',function(){
    	var id = $(this).parent().next().children('.usid').text();
    	var name = $(this).parent().next().children('.usna').text();
    	console.log(id);
    	console.log(name);
    	$('.release').children('input').focus();
    	$('.release').children('input').attr('placeholder','回复:'+name);
    	a++;
    	$('body').on('click','.fabu',function(){
    		console.log("1111111");
    		if(a == 1){
    			var answerDes = $('.huifucon').val();
	          if(answerDes.length!==0){
	            ym.ajax({
	                  url: "/communication/insertConsult",
	                  param:{
	                    	url:urls,
	                      answerDes:answerDes,
	                      typeId:exchangeId,
	                      parentId:id
	                  },
	                  success: function (data) {
	                    window.location.href="topic.html"+"?userid="+userid+"&exchangeId="+exchangeId;
	                  }
	              });
	          }else{
	            $('#ceng').modal('open');
	            $('#info-text').html('回复内容不能为空');
	            window.setTimeout(function(){
	              $('#ceng').modal('close');
	              $('#info-text').html('');
	            },800)
	          }
	    		
    		}
    		
    	})
    })
    
    $('body').on('click','.fabu',function(){
    	console.log("222222");
    	if(a == 0){
    		var answerDes = $('.huifucon').val();
	        if(answerDes.length!==0){
				ym.ajax({
		            url: "/communication/insertConsult",
		            param:{
		            	url:urls,
		                answerDes:answerDes,
		                typeId:exchangeId,
		                answerUserId:1
		            },
		            success: function (data) {
		            	window.location.href="topic.html"+"?userid="+userid+"&exchangeId="+exchangeId;
		            }
		        });
	    }else{
	      $('#ceng').modal('open');
	            $('#info-text').html('回复内容不能为空');
	            window.setTimeout(function(){
	              $('#ceng').modal('close');
	              $('#info-text').html('');
	            },800)
	    	}
	    }
	})
    $('[data-magnify]').Magnify({
    Toolbar: [
      'prev',
      'next',
      'rotateLeft',
      'rotateRight',
      'zoomIn',
      'actualSize',
      'zoomOut'
    ],
    keyboard:true,
    draggable:true,
    movable:true,
    modalSize:[800,600],
    beforeOpen:function (obj,data) {
      console.log('beforeOpen')
    },
    opened:function (obj,data) {
      console.log('opened')
    },
    beforeClose:function (obj,data) {
      console.log('beforeClose')
    },
    closed:function (obj,data) {
      console.log('closed')
    },
    beforeChange:function (obj,data) {
      console.log('beforeChange')
    },
    changed:function (obj,data) {
      console.log('changed')
    }
  });
})
